1
|
|
|
var ExtensibleData = require('./ExtensibleData'), |
2
|
|
|
Person = require('./Person'), |
3
|
|
|
Relationship = require('./Relationship'), |
4
|
|
|
SourceDescription = require('./SourceDescription'), |
5
|
|
|
Agent = require('./Agent'), |
6
|
|
|
Event = require('./Event'), |
7
|
|
|
Document = require('./Document'), |
8
|
|
|
PlaceDescription = require('./PlaceDescription'), |
9
|
|
|
Attribution = require('./Attribution'), |
10
|
|
|
utils = require('./utils'); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A GEDCOM X document. |
14
|
|
|
* |
15
|
|
|
* @constructor |
16
|
|
|
* @param {Object} [json] |
|
|
|
|
17
|
|
|
*/ |
18
|
|
|
var GedcomX = function(json){ |
19
|
|
|
|
20
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
21
|
|
|
if(!(this instanceof GedcomX)){ |
22
|
|
|
return new GedcomX(json); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
26
|
|
|
if(GedcomX.isInstance(json)){ |
27
|
|
|
return json; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
ExtensibleData.call(this, json); |
31
|
|
|
|
32
|
|
|
if(json){ |
|
|
|
|
33
|
|
|
this.setPersons(json.persons); |
34
|
|
|
this.setRelationships(json.relationships); |
35
|
|
|
this.setSourceDescriptions(json.sourceDescriptions); |
36
|
|
|
this.setAgents(json.agents); |
37
|
|
|
this.setEvents(json.events); |
38
|
|
|
this.setDocuments(json.documents); |
39
|
|
|
this.setPlaces(json.places); |
40
|
|
|
this.setAttribution(json.attribution); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
}; |
43
|
|
|
|
44
|
|
|
GedcomX.prototype = Object.create(ExtensibleData.prototype); |
45
|
|
|
|
46
|
|
|
GedcomX._gedxClass = GedcomX.prototype._gedxClass = 'GedcomX'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check whether the given object is an instance of this class. |
50
|
|
|
* |
51
|
|
|
* @param {Object} obj |
52
|
|
|
* @returns {Boolean} |
53
|
|
|
*/ |
54
|
|
|
GedcomX.isInstance = function(obj){ |
55
|
|
|
return utils.isInstance(obj, this._gedxClass); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the persons |
60
|
|
|
* |
61
|
|
|
* @returns {Person[]} |
62
|
|
|
*/ |
63
|
|
|
GedcomX.prototype.getPersons = function(){ |
64
|
|
|
return this.persons || []; |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set the persons |
69
|
|
|
* |
70
|
|
|
* @param {Person[]|Object[]} persons |
71
|
|
|
* @returns {GedcomX} This instance |
72
|
|
|
*/ |
73
|
|
|
GedcomX.prototype.setPersons = function(persons){ |
74
|
|
|
return this._setArray(persons, 'persons', 'addPerson'); |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add a person |
79
|
|
|
* |
80
|
|
|
* @param {Person|Object} |
|
|
|
|
81
|
|
|
* @returns {GedcomX} This instance |
82
|
|
|
*/ |
83
|
|
|
GedcomX.prototype.addPerson = function(person){ |
84
|
|
|
return this._arrayPush(person, 'persons', Person); |
85
|
|
|
}; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the relationships |
89
|
|
|
* |
90
|
|
|
* @returns {Relationship[]} |
91
|
|
|
*/ |
92
|
|
|
GedcomX.prototype.getRelationships = function(){ |
93
|
|
|
return this.relationships || []; |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the relationships |
98
|
|
|
* |
99
|
|
|
* @param {Relationship[]|Object[]} relationships |
100
|
|
|
* @returns {GedcomX} |
101
|
|
|
*/ |
102
|
|
|
GedcomX.prototype.setRelationships = function(relationships){ |
103
|
|
|
return this._setArray(relationships, 'relationships', 'addRelationship'); |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add a relationship |
108
|
|
|
* |
109
|
|
|
* @param {Relationship|Object} relationship |
110
|
|
|
* @returns {GedcomX} |
111
|
|
|
*/ |
112
|
|
|
GedcomX.prototype.addRelationship = function(relationship){ |
113
|
|
|
return this._arrayPush(relationship, 'relationships', Relationship); |
114
|
|
|
}; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the source descriptions |
118
|
|
|
* |
119
|
|
|
* @returns {SourceDescription[]} |
120
|
|
|
*/ |
121
|
|
|
GedcomX.prototype.getSourceDescriptions = function(){ |
122
|
|
|
return this.sourceDescriptions || []; |
123
|
|
|
}; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set the source descriptions |
127
|
|
|
* |
128
|
|
|
* @param {SourceDescription[]|Object[]} sourceDescriptions |
129
|
|
|
* @returns {GedcomX} |
130
|
|
|
*/ |
131
|
|
|
GedcomX.prototype.setSourceDescriptions = function(sourceDescriptions){ |
132
|
|
|
return this._setArray(sourceDescriptions, 'sourceDescriptions', 'addSourceDescription'); |
133
|
|
|
}; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Add a ource description |
137
|
|
|
* |
138
|
|
|
* @param {SourceDescription|Object} sourceDescription |
139
|
|
|
* @returns {GedcomX} |
140
|
|
|
*/ |
141
|
|
|
GedcomX.prototype.addSourceDescription = function(sourceDescription){ |
142
|
|
|
return this._arrayPush(sourceDescription, 'sourceDescriptions', SourceDescription); |
143
|
|
|
}; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the agents |
147
|
|
|
* |
148
|
|
|
* @returns {Agent[]} |
149
|
|
|
*/ |
150
|
|
|
GedcomX.prototype.getAgents = function(){ |
151
|
|
|
return this.agents || []; |
152
|
|
|
}; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the agents |
156
|
|
|
* |
157
|
|
|
* @param {Agent[]|Object[]} agents |
158
|
|
|
* @returns {GedcomX} |
159
|
|
|
*/ |
160
|
|
|
GedcomX.prototype.setAgents = function(agents){ |
161
|
|
|
return this._setArray(agents, 'agents', 'addAgent'); |
162
|
|
|
}; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Add an agent |
166
|
|
|
* |
167
|
|
|
* @param {Agent|Object} |
|
|
|
|
168
|
|
|
* @returns {GedcomX} |
169
|
|
|
*/ |
170
|
|
|
GedcomX.prototype.addAgent = function(agent){ |
171
|
|
|
return this._arrayPush(agent, 'agents', Agent); |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get events |
176
|
|
|
* |
177
|
|
|
* @returns {Event[]} |
178
|
|
|
*/ |
179
|
|
|
GedcomX.prototype.getEvents = function(){ |
180
|
|
|
return this.events || []; |
181
|
|
|
}; |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set events |
185
|
|
|
* |
186
|
|
|
* @param {Event[]|Object[]} events |
187
|
|
|
* @returns {GedcomX} |
188
|
|
|
*/ |
189
|
|
|
GedcomX.prototype.setEvents = function(events){ |
190
|
|
|
return this._setArray(events, 'events', 'addEvent'); |
191
|
|
|
}; |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Add an event |
195
|
|
|
* |
196
|
|
|
* @param {Event|Object} event |
197
|
|
|
* @returns {GedcomX} |
198
|
|
|
*/ |
199
|
|
|
GedcomX.prototype.addEvent = function(event){ |
200
|
|
|
return this._arrayPush(event, 'events', Event); |
201
|
|
|
}; |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get the documents |
205
|
|
|
* |
206
|
|
|
* @returns {Document[]} |
207
|
|
|
*/ |
208
|
|
|
GedcomX.prototype.getDocuments = function(){ |
209
|
|
|
return this.documents || []; |
210
|
|
|
}; |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Set the documents |
214
|
|
|
* |
215
|
|
|
* @param {Documents[]|Object[]} documents |
216
|
|
|
* @returns {GedcomX} |
217
|
|
|
*/ |
218
|
|
|
GedcomX.prototype.setDocuments = function(documents){ |
219
|
|
|
return this._setArray(documents, 'documents', 'addDocument'); |
220
|
|
|
}; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Add a document |
224
|
|
|
* |
225
|
|
|
* @param {Document|Object} doc |
226
|
|
|
* @returns {GedcomX} |
227
|
|
|
*/ |
228
|
|
|
GedcomX.prototype.addDocument = function(doc){ |
229
|
|
|
return this._arrayPush(doc, 'documents', Document); |
230
|
|
|
}; |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get places |
234
|
|
|
* |
235
|
|
|
* @returns {PlaceDescription[]} |
236
|
|
|
*/ |
237
|
|
|
GedcomX.prototype.getPlaces = function(){ |
238
|
|
|
return this.places || []; |
239
|
|
|
}; |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set the places |
243
|
|
|
* |
244
|
|
|
* @param {PlaceDescription[]|Object} places |
245
|
|
|
* @returns {GedcomX} |
246
|
|
|
*/ |
247
|
|
|
GedcomX.prototype.setPlaces = function(places){ |
248
|
|
|
return this._setArray(places, 'places', 'addPlace'); |
249
|
|
|
}; |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Add a place |
253
|
|
|
* |
254
|
|
|
* @param {PlaceDescription} place |
255
|
|
|
* @returns {GedcomX} |
256
|
|
|
*/ |
257
|
|
|
GedcomX.prototype.addPlace = function(place){ |
258
|
|
|
return this._arrayPush(place, 'places', PlaceDescription); |
259
|
|
|
}; |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get attritbution |
263
|
|
|
* |
264
|
|
|
* @returns {Attribution} |
265
|
|
|
*/ |
266
|
|
|
GedcomX.prototype.getAttribution = function(){ |
267
|
|
|
return this.attribution; |
268
|
|
|
}; |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set attribution |
272
|
|
|
* |
273
|
|
|
* @param {Attribution} attribution |
274
|
|
|
* @returns {GedcomX} |
275
|
|
|
*/ |
276
|
|
|
GedcomX.prototype.setAttribution = function(attribution){ |
277
|
|
|
if(attribution){ |
278
|
|
|
this.attribution = Attribution(attribution); |
279
|
|
|
} |
280
|
|
|
return this; |
281
|
|
|
}; |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Export the object as JSON |
285
|
|
|
* |
286
|
|
|
* @return {Object} JSON object |
287
|
|
|
*/ |
288
|
|
|
GedcomX.prototype.toJSON = function(){ |
289
|
|
|
return this._toJSON(ExtensibleData, [ |
290
|
|
|
'persons', |
291
|
|
|
'relationships', |
292
|
|
|
'sourceDescriptions', |
293
|
|
|
'agents', |
294
|
|
|
'events', |
295
|
|
|
'documents', |
296
|
|
|
'places', |
297
|
|
|
'attribution' |
298
|
|
|
]); |
299
|
|
|
}; |
300
|
|
|
|
301
|
|
|
// Expose all classes |
302
|
|
|
GedcomX.Address = require('./Address'); |
303
|
|
|
GedcomX.Agent = Agent; |
304
|
|
|
GedcomX.Attribution = Attribution; |
305
|
|
|
GedcomX.Conclusion = require('./Conclusion'); |
306
|
|
|
GedcomX.Coverage = require('./Coverage'); |
307
|
|
|
GedcomX.Date = require('./Date'); |
308
|
|
|
GedcomX.Document = Document; |
309
|
|
|
GedcomX.Event = Event; |
310
|
|
|
GedcomX.EventRole = require('./EventRole'); |
311
|
|
|
GedcomX.EvidenceReference = require('./EvidenceReference'); |
312
|
|
|
GedcomX.ExtensibleData = ExtensibleData; |
313
|
|
|
GedcomX.Fact = require('./Fact'); |
314
|
|
|
GedcomX.Gender = require('./Gender'); |
315
|
|
|
GedcomX.Identifiers = require('./Identifiers'); |
316
|
|
|
GedcomX.Name = require('./Name'); |
317
|
|
|
GedcomX.NameForm = require('./NameForm'); |
318
|
|
|
GedcomX.NamePart = require('./NamePart'); |
319
|
|
|
GedcomX.Note = require('./Note'); |
320
|
|
|
GedcomX.OnlineAccount = require('./OnlineAccount'); |
321
|
|
|
GedcomX.Person = Person; |
322
|
|
|
GedcomX.PlaceDescription = PlaceDescription; |
323
|
|
|
GedcomX.PlaceReference = require('./PlaceReference'); |
324
|
|
|
GedcomX.Qualifier = require('./Qualifier'); |
325
|
|
|
GedcomX.Relationship = Relationship; |
326
|
|
|
GedcomX.ResourceReference = require('./ResourceReference'); |
327
|
|
|
GedcomX.SourceCitation = require('./SourceCitation'); |
328
|
|
|
GedcomX.SourceDescription = SourceDescription; |
329
|
|
|
GedcomX.SourceReference = require('./SourceReference'); |
330
|
|
|
GedcomX.Subject = require('./Subject'); |
331
|
|
|
GedcomX.TextValue = require('./TextValue'); |
332
|
|
|
|
333
|
|
|
module.exports = GedcomX; |